﻿/*
Commerce by Onex – eCommerce software platform (www.ecommerce.byonex.com)
Copyright (C) 2008-2010 by Onex Group

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 
 */
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ShopModel;
using ShopBLL;


public partial class cms_Vouchers : System.Web.UI.Page
{

    #region VARIABLES

    int PageSize = 50;

    #endregion VARIABLES

    #region METHODS

    protected void _DataBind()
    {
        //if (tcMenu.ActiveTabIndex == 1)
        {
            int orderCount = 0;
            int pageNumber = ctrlPagerUn.PageNumber;

            gvUnused.PageSize = PageSize;
            ctrlPagerUn.PageSize = gvUnused.PageSize;

            List<Voucher> vouchers = OrderManager.Instance.GetVouchers(ref pageNumber, PageSize, out orderCount, false);
            ctrlPagerUn.PageNumber = pageNumber;

            gvUnused.DataSource = vouchers;
            gvUnused.DataBind();
            ctrlPagerUn.ItemCount = orderCount;
        }
        // else if (tcMenu.ActiveTabIndex == 2)
        {
            int orderCount = 0;
            int pageNumber = ctrlPagerU.PageNumber;

            gvUsed.PageSize = PageSize;
            ctrlPagerU.PageSize = gvUsed.PageSize;

            List<Voucher> vouchers = OrderManager.Instance.GetVouchers(ref pageNumber, PageSize, out orderCount, true);
            ctrlPagerU.PageNumber = pageNumber;

            gvUsed.DataSource = vouchers;
            gvUsed.DataBind();
            ctrlPagerU.ItemCount = orderCount;
        }
        BindLastGenerated();
    }

    protected void BindLastGenerated()
    {
        gvLastGenerated.DataSource = OrderManager.Instance.GetLastGeneratedVouchers();
        gvLastGenerated.DataBind();
    }

    #endregion METHODS

    #region EVENTS

    protected void ctrlPager_PageIndexChanged(object sender, GridViewPageEventArgs e)
    {
        _DataBind();
    }

    protected void btnGenerate_Click(object sender, EventArgs e)
    {
        //zapis vouchera
        bool error = false;
        //if (pnlVouchers.Enabled == true && ddlVoucherType.SelectedValue != "-1")
        {
            decimal min = -1m, val = -1m;
            int count = 0;

            if (!int.TryParse(tbAmount.Text, out count))
            {
                error = true;
                CtrlShowInfo.ShowInfo(true, Resources.Language.GiveNumber);
            }

            if (string.IsNullOrEmpty(tbVoucherMinVal.Text)) //nie podano minimalnej kwoty zamówienia
            {
                error = true;
                CtrlShowInfo.ShowInfo(true, Resources.Language.GiveMinVoucherValue);
            }
            else if (string.IsNullOrEmpty(tbVoucherValue.Text) && (ddlVoucherType.SelectedValue == "2" || ddlVoucherType.SelectedValue == "3")) // nie podano wartości dla kuponów kwotowego lub procentowego
            {
                error = true;
                CtrlShowInfo.ShowInfo(true, Resources.Language.GiveVoucherValue);
            }
            else
            {
                Voucher voucher = new Voucher();
                voucher.IdVoucher = -1;
                voucher.Code = new Guid().ToString().Replace("-", "").Substring(0, 8);
                voucher.CustomerId = null;
                voucher.Date = DateTime.Now;
                voucher.GeneratedRealizationId = null;
                voucher.Used = false;
                voucher.UsedRealizationId = null;
                if (ddlVoucherType.SelectedValue == "1") //darmowa wysyłka
                {
                    if (decimal.TryParse(tbVoucherMinVal.Text, out min))
                    {
                        voucher.Type = VoucherType.FreeShipment;
                        voucher.AcceptValue = min;
                        voucher.Value = null;
                    }
                    else
                    {
                        error = true;
                        CtrlShowInfo.ShowInfo(true, Resources.Language.WrongVoucherMinValue);
                    }
                }
                else // procentowy lub kwotowy
                {
                    string errorS = string.Empty;
                    voucher.Type = (VoucherType)Convert.ToInt32(ddlVoucherType.SelectedValue);
                    if (decimal.TryParse(tbVoucherMinVal.Text, out min))
                    {
                        voucher.AcceptValue = min;
                    }
                    else
                    {
                        error = true;
                        errorS += Resources.Language.WrongVoucherMinValue + ", ";
                    }

                    if (decimal.TryParse(tbVoucherValue.Text, out val))
                    {
                        voucher.Value = val;
                    }
                    else
                    {
                        error = true;
                        errorS += Resources.Language.WrongVoucherValue;
                    }

                    if (error)
                    {
                        CtrlShowInfo.ShowInfo(true, errorS.TrimEnd(','));
                    }
                }

                if (!error)
                    if (!OrderManager.Instance.GenerateVouchers(voucher, count))
                    {
                        error = true;
                        CtrlShowInfo.ShowInfo(true, Resources.Language.ProblemWithSaveVoucher);
                    }
            }
        }
        if (!error)
        {
            CtrlShowInfo.ShowInfo(false, Resources.Language.VouchersGenerated);
            _DataBind();
            BindLastGenerated();
        }
    }

    protected void gv_RowCreated(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.Header)
        {
            if ((sender as GridView).ID == "gvUnused")
            {
                CheckBox cb = new CheckBox();
                cb.EnableViewState = false;
                cb.Checked = false;
                cb.ID = "cbUnusedSetAll";
                cb.Attributes.Add("onclick", "SetAll(this,'cbU')");
                e.Row.Cells[6].Controls.Add(cb);
            }
            if ((sender as GridView).ID == "gvLastGenerated")
            {
                CheckBox cb = new CheckBox();
                cb.EnableViewState = false;
                cb.Checked = false;
                cb.ID = "cbGenerationSetAll";
                cb.Attributes.Add("onclick", "SetAll(this,'cbG')");
                e.Row.Cells[6].Controls.Add(cb);
            }
        }
    }

    #endregion EVENTS

    #region PAGE EVENTS

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
            ddlVoucherType.Items.AddRange(OrderManager.Instance.GetAllVoucherTypes().ToArray());
        string[] ctlPostBack = Request.Form.GetValues("__EVENTTARGET");
        if (!(ctlPostBack != null && ctlPostBack.Length > 0 && ctlPostBack[0].Contains(controls_ctrlPager.LINK_PAGER_NAME)))
        {
            _DataBind();
        }
    }

    #endregion PAGE EVENTS

}
